package com.example.demoa;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

class StudentAdapter extends RecyclerView.Adapter<StudentViewHolder> {
    private ArrayList<Student> studentArrayList;
    private IOnDelListener delListener;
    private IOnClickItemListener clickItemListener;

    public StudentAdapter(ArrayList<Student> studentArrayList, IOnDelListener delListener,
                          IOnClickItemListener clickItemListener) {
        this.studentArrayList = studentArrayList;
        this.delListener = delListener;
        this.clickItemListener = clickItemListener;
    }

    @NonNull
    @Override
    public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.list_item, parent, false);

        StudentViewHolder studentViewHolder = new StudentViewHolder(view);

        return studentViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final StudentViewHolder holder, final int position) {
        final Student student = studentArrayList.get(position);
        holder.tv_name.setText(student.getName());
        holder.tv_age.setText(String.valueOf(student.getAge()));
        Glide.with(holder.iv_img.getContext()).load(student.getHeadimg().getUrl())
                .into(holder.iv_img);

        holder.iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delListener.del(student.getObjectId());
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickItemListener.click(holder.itemView, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return studentArrayList.size();
    }
}
